﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Content;
using DotNetNuke.Entities.Content.Common;

namespace Christoc.Modules.CouponsDiscounts.Components.Taxonomy
{
    public class Content
    {
        private const string ContentTypeName = "CouponsDiscountsTask";

        public ContentItem CreateContentItem(Transaction objTransaction, int tabId)
        {
            var typeController = new ContentTypeController();
            var colContentTypes = (from t in typeController.GetContentTypes() where t.ContentType == ContentTypeName select t);
            int contentTypeId;

            if (colContentTypes.Count() > 0)
            {
                var contentType = colContentTypes.Single();                
                contentTypeId = contentType == null ? CreateContentType() : contentType.ContentTypeId;
            }
            else
            {
                contentTypeId = CreateContentType();
            }

            var objContent = new ContentItem
            {
                Content = string.Format("{0} {1}", objTransaction.TransactionId, objTransaction.TransactionDate.ToString()),
                ContentTypeId = contentTypeId,
                Indexed = false,
                ContentKey = "tid-" + objTransaction.TransactionDate,
                TabID = tabId
            };

            objContent.ContentItemId = Util.GetContentController().AddContentItem(objContent);

            //Add terms
            var cntTerm = new Terms();
            cntTerm.ManageTransactionTerms(objTransaction, objContent);

            return objContent;
        }

        public void UpdateContentItem(Transaction objTransaction, int tabId)
        {
            var objContent = Util.GetContentController().GetContentItem(objTransaction.ContentItemId);

            if (objContent == null)
            {
                return;
            }

            objContent.Content = string.Format("{0} {1}", objTransaction.TransactionId, objTransaction.TransactionDate.ToString());
            objContent.TabID = tabId;
            Util.GetContentController().UpdateContentItem(objContent);

            var cntTer = new Terms();
            cntTer.ManageTransactionTerms(objTransaction, objContent);
        }

        public void DeleteContentType(Transaction objTransaction)
        {
            if (objTransaction.ContentItemId <= Null.NullInteger)
            {
                return;
            }

            var objContent = Util.GetContentController().GetContentItem(objTransaction.ContentItemId);
            if (objContent == null)
            {
                return;
            }

            var cntTerms = new Terms();
            cntTerms.RemoveTransactionTerms(objTransaction);

            Util.GetContentController().DeleteContentItem(objContent);
        }

        private int CreateContentType()
        {
            var typeController = new ContentTypeController();
            var objContentType = new ContentType { ContentType = ContentTypeName };

            return typeController.AddContentType(objContentType);
        }
    }
}